/**
 * 
 */
package com.nh.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.hibernate.Criteria;

import com.nh.entity.NgLogUser;
import com.nh.entity.NgUser;

/**
 * 项目名称：NotoginsengManage
 * 
 * 类名称：CountAction
 * 
 * 类描述：
 * 
 * 创建人：李兴建
 * 
 * E-mail:lxj08193@163.com
 * 
 * 创建时间：2012-11-11 下午03:16:12
 * 
 * 修改人：李兴建
 * 
 * 修改时间：2012-11-11 下午03:16:12
 * 
 * 修改备注：
 * 
 * @version
 */
public class CountAction extends BaseAction {

	private NgUser user;
	private NgLogUser loguser;

	public NgUser getUser() {
		return user;
	}

	public void setUser(NgUser user) {
		this.user = user;
	}

	public NgLogUser getLoguser() {
		return loguser;
	}

	public void setLoguser(NgLogUser loguser) {
		this.loguser = loguser;
	}

	/**
	 * 活跃用户统计
	 * 
	 * @return
	 */
	public String activeCountPage() {
		
		return "activeCountPage";
	}
	
	/**
	 * 活跃用户统计
	 * 
	 * @return
	 */
	public String visitPage() {
		
		return "visitPage";
	}

	/**
	 * 统计
	 * 
	 * @return
	 */
	public void registerCountPage() {
		String js="";
		HttpServletResponse resp = ServletActionContext.getResponse();
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("text/html; charset=utf-8");
		HttpServletRequest request=ServletActionContext.getRequest();
		String type=request.getParameter("type");
		String years=request.getParameter("years");
		Criteria c=null;
		//type=0注册用户统计
		//type=1用户访问统计
		if(type.equals("0"))
		{
			c=this.createCriteria(NgUser.class);
			js = registerChart(years,c);
		}
		else if(type.equals("1"))
		{
			c=this.createCriteria(NgLogUser.class);
			js = visitChart(years,c);
		}
		
		PrintWriter out = null;
		try {
			out = resp.getWriter();

			out.write(js);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (out != null) {
				out.close();
			}
		}
	}

	
	
	
	private String visitChart(String year,Criteria criteria)
	{
		List<NgLogUser> user=criteria.list();
		String js = "";
		if(null!=user)
		{
			
			List<String> listKey=new ArrayList<String>();
//			List<String> listValue=new ArrayList<String>();
			DateFormat df = new SimpleDateFormat("yyyy");
			Map<String, String> map=new HashMap<String, String>();
			List<Map<String, Integer>> listValue=new ArrayList<Map<String,Integer>>();
			for(int i=0;i<user.size();i++)
			{
				map.put(user.get(i).getVisitName(), null);
			}
			for(String key:map.keySet())
			{
				listKey.add(key);
			}
			for(int i=0;i<listKey.size();i++)
			{
				String value="";
				Map<String, Integer> mapmonth = month();
				Map<String, Integer> mapvalue = new HashMap<String, Integer>();
				for(int j=0;j<user.size();j++)
				{
					if(user.get(j).getVisitName().equals(listKey.get(i)))
					{
						DateFormat dfmonth = new SimpleDateFormat("MM");
						String month = dfmonth.format(user.get(j).getLongTime());
						String years=df.format(user.get(j).getLongTime());
						if(year.equals(years)) mapmonth.put(month,mapmonth.get(month)+1 );
					}
				}
				for(String mapkey:mapmonth.keySet())
				{
					mapvalue.put(mapkey, mapmonth.get(mapkey));
//					value+=mapmonth.get(mapkey)+",";
				}
				listValue.add(mapvalue);
			}
			
			
			js += "var chart= new Highcharts.Chart( {";
			js += "chart : {";
			js += "renderTo : 'container',";
			js += "type : 'column'},";
			js += "title : {text : '" + year + "年手机客户端信息访问量统计'},";
			js += "subtitle : {text : '来自: " + year + "年文山三七手机助手手机客户端信息访问量统计'},";
			js += "xAxis : {categories : [ '一月', '二月', '三月', '四月', '五月','六月', '七月', '八月', '九月', '十月', '十一月','十二月' ]},";
			js += "yAxis : {min : 0,title : {text : '单位 (个)'}},";
			js += "legend : {layout : 'vertical',backgroundColor : '#FFFFFF',align : 'left',verticalAlign : 'top',x : 100,y : 70,floating : true,shadow : true},";
			js += "tooltip : {formatter : function() {return '' + this.x + ': ' + this.y + ' 个';}},";
			js += "plotOptions : {column : {pointPadding : 0.2,borderWidth : 0}},";
			js += "series : [ " ;
			for(int i=0;i<listKey.size();i++)
			{
				
				js+="{";
				js+="name:'"+listKey.get(i)+"',";
				js+="data: ["
				+ listValue.get(i).get("01") + ","
				+ listValue.get(i).get("02") + "," + listValue.get(i).get("03") + "," + listValue.get(i).get("04")
				+ "," + map.get("05") + "," + listValue.get(i).get("06") + ","
				+ listValue.get(i).get("07") + "," + listValue.get(i).get("08") + "," + listValue.get(i).get("09")
				+ "," + listValue.get(i).get("10") + "," + listValue.get(i).get("11") + ","
				+ listValue.get(i).get("12") ;
//				js+=listValue.get(i);
				if(i!=listKey.size()-1)js+="]},";
				else js+="]}";
			}
			js +="]";
			js += "";
			js += "});";
			
		}
		return js;
	}
	
	/**
	 * 统计注册用户
	 * @param year 更具年份统计
	 * @param criteria 数据源
	 * @return 返回统计JS
	 */
	private String registerChart(String year,Criteria criteria) {
		List<NgUser> u=criteria.list();
		String js = "";
		if (null != u) {
			DateFormat df = new SimpleDateFormat("yyyy");
//			String year = df.format(d);
			Map<String, Integer> map = month();
			for (int i = 0; i < u.size(); i++) {
				DateFormat dfmonth = new SimpleDateFormat("MM");
				String month = dfmonth.format(u.get(i).getRegisterTime());
				String years=df.format(u.get(i).getRegisterTime());
				if(year.equals(years))
				{
					map.put(month, map.get(month) + 1);
				}
				
			}
			js += "var chart= new Highcharts.Chart( {";
			js += "chart : {";
			js += "renderTo : 'container',";
			js += "type : 'column'},";
			js += "title : {text : '" + year + "年手机客户端活跃用户统计'},";
			js += "subtitle : {text : '来自: " + year + "年文山三七手机助手手机客户端活跃用户统计'},";
			js += "xAxis : {categories : [ '一月', '二月', '三月', '四月', '五月','六月', '七月', '八月', '九月', '十月', '十一月','十二月' ]},";
			js += "yAxis : {min : 0,title : {text : '单位 (个)'}},";
			js += "legend : {layout : 'vertical',backgroundColor : '#FFFFFF',align : 'left',verticalAlign : 'top',x : 100,y : 70,floating : true,shadow : true},";
			js += "tooltip : {formatter : function() {return '' + this.x + ': ' + this.y + ' 个';}},";
			js += "plotOptions : {column : {pointPadding : 0.2,borderWidth : 0}},";
			js += "series : [{name : '活跃用户',data : [ " + map.get("01") + ","
					+ map.get("02") + "," + map.get("03") + "," + map.get("04")
					+ "," + map.get("05") + "," + map.get("06") + ","
					+ map.get("07") + "," + map.get("08") + "," + map.get("09")
					+ "," + map.get("10") + "," + map.get("11") + ","
					+ map.get("12") + "]}]";
			js += "";
			js += "});";
		}
		return js;
	}

	public Map<String, Integer> month() {
		Map<String, Integer> map = new HashMap<String, Integer>();
		map.put("01", 0);
		map.put("02", 0);
		map.put("03", 0);
		map.put("04", 0);
		map.put("05", 0);
		map.put("06", 0);
		map.put("07", 0);
		map.put("08", 0);
		map.put("09", 0);
		map.put("10", 0);
		map.put("11", 0);
		map.put("12", 0);
		return map;
	}

	public static void main(String[] arg) {
		Date d = new Date();
		DateFormat df = new SimpleDateFormat("MM");
		System.out.print(df.format(d));
	}

}
